<template>
  <div class="all">
    <div class="app">
      <div>
        <el-form ref="form" :model="form" label-width="80px" inline>
          <el-form-item>
            <el-select
              v-model="form.region"
              placeholder="请选择查询条件"
              class="query"
            >
              <el-option label="服务名" value="serviceId"></el-option>
              <el-option label="用户名" value="userId"></el-option>
              <el-option label="店铺名" value="shopId"></el-option>
              <!-- <el-option label="预约时间" value="orderTime"></el-option> -->
              <!-- <el-option label="下单时间" value="time"></el-option> -->
              <!-- <el-option label="订单状态" value="state"></el-option> -->
            </el-select>
            <el-form-item>
              <el-input
                v-model="form.name"
                placeholder="选择后输入查询内容"
                :disabled="form.region == '' ? true : false"
              ></el-input>
            </el-form-item>
            <!-- <el-button type="primary" class="referBtn el-icon-search" @click="searchHandle"
              > 搜索</el-button
            > -->
            <el-button type="primary" class="referBtn" @click="resetHandle"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div>
      <el-table
        ref="filterTable"
        :border="true"
        :data="
          tableData.filter(
            (data) =>
              !form.name ||
              data[form.region].name
                .toLowerCase()
                .includes(form.name.toLowerCase())
          )
        "
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="50" align="center">
        </el-table-column>
        <el-table-column
          prop="serviceId.name"
          label="服务名"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="userId.name" label="用户名" align="center">
        </el-table-column>
        <el-table-column
          prop="shopId.name"
          label="所属店铺"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="time" label="下单时间" align="center">
        </el-table-column>
        <el-table-column prop="orderTime" label="预约时间" align="center">
        </el-table-column>
        <el-table-column
          prop="state"
          label="订单状态"
          width="100"
          align="center"
          :filters="[
            { text: '待付款', value: '待付款' },
            { text: '待完成', value: '待完成' },
            { text: '待评价', value: '待评价' },
            { text: '已完成', value: '已完成' },
          ]"
          :filter-method="filterTag"
          filter-placement="bottom-end"
        >
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.state === '待评价'
                  ? 'info'
                  : 'success' && scope.row.state === '待付款'
                  ? 'danger'
                  : 'success' && scope.row.state === '待完成'
                  ? 'warning'
                  : 'success'
              "
              disable-transitions
              >{{ scope.row.state }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              class="el-icon-edit"
              type="primary"
            >
              状态更改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      title="状态更改"
      :visible.sync="dialogVisible"
      width="20%"
      :before-close="handleClose"
    >
      <el-select v-model="boxValue" placeholder="请选择" class="box">
        <el-option
          v-for="(item, index) in options"
          :key="index"
          :label="item.text"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmHandle()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 引入 axios

export default {
  data() {
    return {
      dialogVisible: false, // 弹出框状态
      form: {
        region: "", // 查询选择项
        name: "", // 查询内容
      },
      options: [
        { text: "待付款", value: "待付款" },
        { text: "待完成", value: "待完成" },
        { text: "待评价", value: "待评价" },
        { text: "已完成", value: "已完成" },
      ],
      boxValue: "", //弹出框选择
      redactInfo: "", //编辑信息
    };
  },

  mounted() {
    let obj = {
      typeId: "adminId",
      valueId: localStorage.getItem("shopAdminId"),
    };
    this.search(obj); // 调用查询请求参函数
  },

  computed: {
    // 数据数组
    tableData() {
      return this.$store.getters.gettableDataServicesGetters;
    },
    // 分页数据
    page() {
      return this.$store.getters.getPageServicesGetters;
    },
  },

  methods: {
    // 查询请求参函数
    search(obj) {
      this.$store.dispatch("tableDataServicesActions", obj);
    },

    // 查询选项
    filterTag(value, row) {
      return row.state === value;
    },

    // 显示条数更换
    handleSizeChange(val) {
      let obj = {
        size: val,
        typeId: "adminId",
        valueId: localStorage.getItem("shopAdminId"),
      };
      this.search(obj);
    },

    // 每页点击更换
    handleCurrentChange(val) {
      let obj = {
        current: val,
        typeId: "adminId",
        valueId: localStorage.getItem("shopAdminId"),
      };
      this.search(obj);
    },

    // 重置按钮
    resetHandle() {
      this.form.region = this.form.name = "";
      // this.search();
    },

    // 编辑按钮
    handleEdit(index, row) {
      this.redactInfo = row; // 当前行信息
      this.boxValue = "";
      this.dialogVisible = true;
    },

    // 弹出框右上角关闭
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },

    // 弹出框确认按钮
    confirmHandle() {
      if (this.boxValue) {
        this.redactInfo.state = this.boxValue;
      }

      let obj = {
        id: this.redactInfo._id,
        state: this.redactInfo.state,
      };
      let arr = [{ id: this.redactInfo._id }, { state: this.redactInfo.state }];
      this.$store.dispatch("putServicesActions", arr);

      this.dialogVisible = false;
    },
  },
};
</script>

<style scoped>
.all {
  margin: 50px;
}
.app {
  display: flex;
  /* justify-content: flex-end; */
}

.query {
  width: 150px;
  margin-right: 10px;
}
.box {
  margin-left: 10%;
  width: 80%;
}
.block {
  margin-top: 20px;
}
</style>
